import React,{Component} from 'react'

import {
    View,
    Text,
    FlatList,
    StyleSheet,
    Image,
    ActivityIndicator,
    Alert,
    ToastAndroid,
    Dimensions,
    ScrollView,
    TouchableHighlight,
    TouchableNativeFeedback
}from 'react-native'

import loading from '../../commons/loading'


const { width, height } = Dimensions.get('window');

const styles=StyleSheet.create({
    loading: {
        marginTop: 100,
    },
    topclick:{
        width:'100%',
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center',
        height:50,
    },
    addtop:{
        width:'100%',
        height:50,
        borderRadius:5,
        backgroundColor:'rgb(255,97,3)',
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center'
    },
    itemwarp:{
        width:'100%',
        backgroundColor:'white',
        height:50,
        flexDirection:'row',
        justifyContent:'space-around',
        alignItems:'center'
    },
    showleft:{
        width:'20%',
        height:'100%',
        textAlign:'center',
        lineHeight:35,
        borderRightWidth:1,
        borderColor:'rgba(150,150,150,0.3)'
    },
    showmiddle:{
        width:'60%',
        height:'100%',
        flexDirection:'row',
        justifyContent:'space-around',
        alignItems:'center'
    },
    showright:{
        width:'20%',
        height:'100%',
        textAlign:'center',
        lineHeight:35,
        borderLeftWidth:1
    },
    foodimg:{
        width:40,
        height:40,
    },
    fooddesc:{
        width:100,
        height:'80%',
        flexDirection:'column',
        justifyContent:'space-around',
        alignItems:'center'
    },
    middlewarp:{
        width:'60%',
        height:'100%',
        flexDirection:'row',
        justifyContent:'center',
        alignItems:'center'
    },
    jiange:{
        width:'100%',
        borderTopWidth:1,
        borderColor:'rgba(150,150,150,0.2)',
        height:1
    }

})



export default class Foodgood extends Component{
    static navigationOptions={
        title:'所有菜品',
        headerTitleStyle: {
        alignSelf:'center'
      },
    };

state={
    allfood:[],
    havefood:false
}

    changeflag=async function(flag,id){
        try{
            var response= await fetch(global.mainurl+'/bossHelper/soldout?fmid='+id+'&soldflag='+flag+'&userid='+global.userdata.id+'&token='+global.token);
            var result=await response.json()
            if(result.code==1){
                ToastAndroid.show('修改成功', ToastAndroid.SHORT);
                this.loading()
            }
            else{
                 ToastAndroid.show('修改失败', ToastAndroid.SHORT);
            }

        }catch(error){
             ToastAndroid.show('网络出错', ToastAndroid.SHORT);
        }

    }

    getallfood=async function(){
        try {
            let response = await fetch(global.mainurl+"bossHelper/getAllFoods?sellerid="+global.sellerid+"&userid="+global.userdata.id+"&token="+global.token);
            var data=await response.json()
            console.log(data)
            console.log(12)
            if(data.code==1){
                       return data.data
                    }
            else{
								
						}
            } 
    catch(error) {
      console.log(error)
            ToastAndroid.show('网络出错', ToastAndroid.SHORT);
            }
    }

    getallfoodtype=async function(){
        
        try {
            let response = await fetch(global.mainurl+"bossHelper/getFoodType?sellerid="+global.sellerid+"&userid="+global.userdata.id+"&token="+global.token);
            var data=await response.json()
            console.log(data)
            if(data.code==1){
                global.foodtype=data.data
                var order_foodtype = new Array()
						for (var i = 0; i < data.data.length; i++) {
							order_foodtype.push({
								index : i,
								name : data.data[i].name,
								num : 0,
								id : data.data[i].id,
								list : []
							})
						}
                        return order_foodtype
            }
            else{
								
						}
            } 
    catch(error) {
      console.log(error)
            ToastAndroid.show('网络出错', ToastAndroid.SHORT);
            }
    }

        loading=async function(){
                        var type= await this.getallfoodtype()
                    var food= await this.getallfood()
                    var order_allfoods = new Array()
                            for (var i = 0; i < food.length; i++) {
                                        if(food[i].foodInfoList){
                                        for(var z=0;z<food[i].foodInfoList.length;z++){
                                            food[i].foodInfoList[z].num=0;
                                            food[i].foodInfoList[z].foodname=food[i].foodname;
                                        }
                                        order_allfoods.push({
                                            id : food[i].id,
                                            typeid : food[i].foodtype.id,
                                            listName : food[i].foodname,
                                            flag:food[i].soldflag,
                                            num : 0,
                                            name:'',
                                            src : food[i].foodimage1,
                                            orprice : food[i].foodInfoList[0].price,
                                            price : food[i].foodInfoList[0].nowprice,
                                            listin:food[i].foodInfoList
                                        })
                                        }
                                        else{
                                            order_allfoods.push({
                                            id : food[i].id,
                                            typeid : food[i].foodtype.id,
                                            listName : food[i].foodname,
                                            flag:food[i].soldflag,
                                            num : 0,
                                            name:'',
                                            src : food[i].foodimage1,
                                            orprice : 0,
                                            price : 0,
                                            listin:[]
                                        })
                                        }
                                    }
                    for (var i = 0; i <type.length ; i++) {
                        for (var j = 0; j < order_allfoods.length; j++) {
                            if (order_allfoods[j].typeid == type[i].id) {
                                order_allfoods[j].name=type[i].name
                            }
                        }
                    }
                    this.setState({
                        allfood:order_allfoods,
                        havefood:true
                    })
        }
    
  componentWillMount(){
            this.loading()

    }

    render(){
        return(
            <View>
            <TouchableHighlight style={styles.topclick} onPress={()=>this.props.navigation.navigate("FoodGoodsAdd",{reload:()=>{this.loading()}})}>
            <View style={styles.addtop}>
            <Text style={{color:'white',fontSize:15}}>添加菜品</Text>
            </View>
            </TouchableHighlight>
            {
                this.state.havefood
                ?
                <View style={{paddingBottom:100}}>
            <FlatList
            data={this.state.allfood}
            keyExtractor={(item, index) => index}
            ItemSeparatorComponent={()=>{
                return(
                    <View style={styles.jiange}/>
                    )
                
            }}
            renderItem={({item})=>{
                return(
                    
                    <View style={styles.itemwarp}>

                    <Text style={styles.showleft}>
                    
                    {item.name}
                   
                    </Text>
                    <TouchableNativeFeedback style={styles.showmiddle}
                    onPress={()=>{
                        console.log(item)
                        this.props.navigation.navigate("FoodGoodsAdd",{old:item,reload:()=>{this.loading()}})
                    }}
                    >
                    <View style={styles.middlewarp}>
                        <Image
                        borderRadius={5}
                        source={{ uri:'http://www.dadu999.com'+ item.foodimage1 }}
                        style={styles.foodimg}
                        />

                        <View style={styles.fooddesc}>
                        <Text numberOfLines={1}>
                        {item.listName}
                        </Text>
                        <Text numberOfLines={1}>
                        {item.price}
                        </Text>
                        </View>
                        </View>
                    </TouchableNativeFeedback>


                    {
                     item.flag==0?
                     <TouchableNativeFeedback
                     onPress={
                         ()=>{
                        Alert.alert(
                            '确定下架该菜品？',
                            null,
                            [
                            {text: '取消', onPress: () => {}},
                            {text: '确定', onPress: () => this.changeflag(1,item.id)},
                            {text: '', onPress: () =>{} },
                            ]
                        )
                    }
                     }
                     >
                     <View style={{width:'20%',height:'100%',backgroundColor:"#1296db"}}>   
                            <Text style={{width:'100%',height:'100%',lineHeight:35,textAlign:'center'}}>
                            下架
                            </Text>
                    </View>
                    </TouchableNativeFeedback>
                    :
                    <TouchableNativeFeedback
                     onPress={
                         ()=>{
                        Alert.alert(
                            '确定上架该菜品？',
                            null,
                            [
                            {text: '取消', onPress: () => {}},
                            {text: '确定', onPress: () => this.changeflag(0,item.id)},
                            {text: '', onPress: () =>{} },
                            ]
                        )
                    }
                         }
                     >
                     <View style={{width:'20%',height:'100%'}}>
                            <Text style={{width:'100%',height:'100%',lineHeight:35,textAlign:'center'}}>
                            上架
                            </Text>
                    </View>
                    </TouchableNativeFeedback>
                    }

                    </View>
                )
            }}
            />
            </View>
                :
                <ActivityIndicator size="large" style={styles.loading} />
            }
            
          
            </View>
        )
    }
}